# 根据图片，在Edge浏览器中自动滚动查找图片位置
import cv2
import numpy as np
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time

# 读取图片
target_image_path = 'path_to_your_image.png'
target_image = cv2.imread(target_image_path, 0)

# 打开浏览器
driver = webdriver.Chrome()
driver.get('file:///C:/Users/Administrator/%E6%A1%8C%E9%9D%A2/%E7%94%A8%E6%88%B7%E5%A4%B4%E5%83%8F%E7%9F%A9%E9%98%B5.html')  # 替换为你要查找图片的网页

# 滚动查找图片位置
found = False
while not found:
    # 截取当前浏览器视图的截图
    screenshot = driver.get_screenshot_as_png()
    screenshot = np.frombuffer(screenshot, np.uint8)
    screenshot = cv2.imdecode(screenshot, cv2.IMREAD_GRAYSCALE)

    # 使用OpenCV进行图像匹配
    result = cv2.matchTemplate(screenshot, target_image, cv2.TM_CCOEFF_NORMED)
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)

    # 如果匹配度高于某个阈值，则认为找到了图片
    if max_val > 0.8:
        found = True
        print(f"图片位置: {max_loc}")
    else:
        # 向下滚动页面
        driver.find_element(By.TAG_NAME, 'body').send_keys(Keys.PAGE_DOWN)
        time.sleep(1)  # 等待页面加载

# 关闭浏览器
driver.quit()